<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Http\Requests\GoodsPostRequest;


//引入goods类
use App\Models\Goods;

class GoodsController extends Controller
{
    //后台商品添加页面
    public function getAdd()
    {	
    	//在显示添加模板是要有分类列表
    	// 获取格式化分类数据
    	$cates = CateController::getCates();
    	//解析模板
    	return view('admin.goods.add',['cates'=>$cates]);
    }

    //
    public function postInsert(GoodsPostRequest $request)
    {
    	// dd($request->all());
        //实例化模型对象   -----注意命名空间
        $goods = new Goods;
        //处理数据
        $goods->title = $request->input('title');
        $goods->price = $request->input('price');
        $goods->kucun = $request->input('kucun');
        $goods->content = $request->input('content');
        $goods->cate_id = $request->input('cate_id');
        $goods->status = 1;
        //处理图片上传
        $goods->pic = $this->upload($request);

        //执行数据插入
        $res = $goods->save();
        // dd($res);

        //判断
        if($res){
            return redirect('/admin/goods/index')->with('success','添加成功');
        }else{
            return back()->with('error','数据添加失败');
        }

    }

    //处理图片上传
    public function upload(Request $request)
	{	
		// dd($request->all());

		//检测是否有上传文件
        if($request->hasFile('pic')){
            //文件名
            $name = md5(time()+rand(111111,8999999));
            //文件后缀名获取
            $suffix = $request->file('pic')->getClientOriginalExtension();
            $arr = ['jpg','png','gif'];
            //判断文件上传类型
            if(!in_array($suffix,$arr)){
                echo '上传文件不符合要求';die;
            }
            //将上传文件移动到指定位置
            $request->file('pic')->move('./goods/',$name.'.'.$suffix);
            //将文件路径及文件名返回
            return '/goods/'.$name.'.'.$suffix;
        }else{
        	//判断是不是修改头像过来的请求
        	if(!empty($request->input('oldpic'))){
        		//如果用户没有修改修改  则返回原来的头像路径
        		return $request->input('oldpic');
        	}
        }
	}

    //商品列表
    public function getIndex(Request $request)
    {   
        //查询数据
        $goods = Goods::OrderBy('id','desc')
        ->where(function($query) use($request){
            //判断是否有where条件
            if($request->input('keywords')){
                $query->where('title','like',''.$request->input('keywords').'%');
            }
        })->paginate($request->input('num',10));
        // dd($goods);
        return view('admin.goods.index',[
            'goods'=>$goods,'request'=>$request->all()]);
    }

    //商品删除
    public function getDelete(Request $request)
    {
        $id = $request->input('id');
        //执行删除
        $goods = Goods::find($id);

        $res = $goods->delete();

        if($res){
            return redirect('/admin/goods/index')->with('success','删除成功');
        }else{
            return back()->with('error','数据删除失败');
        }
    }

    //商品修改
    public function getEdit(Request $request)
    {
        //查询
        $goods = Goods::find($request->input('id'));
        //获取格式化数据
        $cates = CateController::getCates();
        //解析模板  分配数据
        return view('admin.goods.edit',['goods'=>$goods,'cates'=>$cates]);
    }

    //执行数据修改
    public function postUpdate(Request $request)
    {
        // dd($request->all());
        $goods = new Goods;

        $goods = Goods::find($request->input('id'));

        //处理数据
        $goods->title = $request->input('title');
        $goods->price = $request->input('price');
        $goods->kucun = $request->input('kucun');
        $goods->content = $request->input('content');
        $goods->cate_id = $request->input('cate_id');
        $goods->status = 1;
        //处理图片上传  头像修改或没修改在upload方法里判断了
        $goods->pic = $this->upload($request);

        // dd($goods);
        
        //执行修改
        $res = $goods->save();
        if($res){
            return redirect('/admin/goods/index')->with('success','修改成功');
        }else{
            return back()->with('error','数据修改失败');
        }
    }
}
